package gui.customizepanel;

import java.awt.Component;
import java.awt.Dimension;

import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;

public class JComposePanel extends JPanel {

	private static final long serialVersionUID = 0xA1L;

	private JButton btn_attach;
	private JButton btn_remove_selected;
	private JButton btn_send;
	private JEditorPane editor_pane;
	private JSeparator jsp1;
	private JSeparator jsp2;
	private JLabel lbl_from;
	private JLabel lbl_subject;
	private JLabel lbl_to;
	private JList<String> list_attachment;
	private JPanel pn_body;
	private JPanel pn_header;
	private JScrollPane scroll_attachment;
	private JScrollPane scroll_editor;
	private JTextField tf_from;
	private JTextField tf_subject;
	private JTextField tf_to;

	private static JComposePanel instance;

	public static final JComposePanel getInstance() {
		if (instance == null)
			return instance = new JComposePanel();
		return instance;
	}

	private JComposePanel() {
		initComponents();
	}

	private void newComps() {
		pn_header = new JPanel();
		lbl_from = new JLabel();
		lbl_to = new JLabel();
		lbl_subject = new JLabel();
		tf_subject = new JTextField();
		tf_to = new JTextField();
		tf_from = new JTextField();
		jsp1 = new JSeparator();
		scroll_attachment = new JScrollPane();
		list_attachment = new JList<>();
		jsp2 = new JSeparator();
		btn_attach = new JButton();
		btn_remove_selected = new JButton();
		btn_send = new JButton();
		pn_body = new JPanel();
		scroll_editor = new JScrollPane();
		editor_pane = new JEditorPane();
	}

	@SuppressWarnings({ "unchecked", "rawtypes", "serial" })
	private void setCompsProperties() {
		pn_header.setBorder(BorderFactory.createTitledBorder(""));
		pn_header.setMinimumSize(new Dimension(1100, 121));
		pn_header.setPreferredSize(new Dimension(1100, 121));

		lbl_from.setText("From");

		lbl_to.setText("To");

		lbl_subject.setText("Subject");

		jsp1.setOrientation(SwingConstants.VERTICAL);

		list_attachment.setModel(new AbstractListModel() {
			String[] strings = { "Item 1", "Item 2", "Item 3" };

			public int getSize() {
				return strings.length;
			}

			public Object getElementAt(int i) {
				return strings[i];
			}
		});
		scroll_attachment.setViewportView(list_attachment);

		jsp2.setOrientation(SwingConstants.VERTICAL);

		btn_attach.setText("Attach File");

		btn_remove_selected.setText("Remove Selcted");

		btn_send.setText("Send");
	}

	private void pn_headerLayout() {
		GroupLayout pn_headerLayout = new GroupLayout(pn_header);
		pn_header.setLayout(pn_headerLayout);
		pn_headerLayout
				.setHorizontalGroup(pn_headerLayout
						.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addGroup(
								pn_headerLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												pn_headerLayout
														.createParallelGroup(
																GroupLayout.Alignment.LEADING)
														.addComponent(
																lbl_subject)
														.addComponent(lbl_to)
														.addComponent(lbl_from))
										.addPreferredGap(
												LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												pn_headerLayout
														.createParallelGroup(
																GroupLayout.Alignment.LEADING)
														.addGroup(
																pn_headerLayout
																		.createParallelGroup(
																				GroupLayout.Alignment.TRAILING)
																		.addComponent(
																				tf_to,
																				GroupLayout.Alignment.LEADING,
																				GroupLayout.PREFERRED_SIZE,
																				450,
																				GroupLayout.PREFERRED_SIZE)
																		.addComponent(
																				tf_subject,
																				GroupLayout.PREFERRED_SIZE,
																				450,
																				GroupLayout.PREFERRED_SIZE))
														.addComponent(
																tf_from,
																GroupLayout.PREFERRED_SIZE,
																536,
																GroupLayout.PREFERRED_SIZE))
										.addGap(0, 6, Short.MAX_VALUE)
										.addComponent(jsp1,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(scroll_attachment,
												GroupLayout.PREFERRED_SIZE,
												205, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												pn_headerLayout
														.createParallelGroup(
																GroupLayout.Alignment.LEADING,
																false)
														.addComponent(
																btn_attach,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(
																btn_remove_selected))
										.addPreferredGap(
												LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(jsp2,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(btn_send,
												GroupLayout.PREFERRED_SIZE,
												109, GroupLayout.PREFERRED_SIZE)
										.addContainerGap()));

		pn_headerLayout.linkSize(SwingConstants.HORIZONTAL, new Component[] {
				tf_from, tf_subject, tf_to });

		pn_headerLayout
				.setVerticalGroup(pn_headerLayout
						.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addGroup(
								pn_headerLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												pn_headerLayout
														.createParallelGroup(
																GroupLayout.Alignment.LEADING)
														.addComponent(
																jsp1,
																GroupLayout.Alignment.TRAILING)
														.addGroup(
																pn_headerLayout
																		.createSequentialGroup()
																		.addGroup(
																				pn_headerLayout
																						.createParallelGroup(
																								GroupLayout.Alignment.LEADING,
																								false)
																						.addGroup(
																								pn_headerLayout
																										.createSequentialGroup()
																										.addComponent(
																												btn_attach)
																										.addPreferredGap(
																												LayoutStyle.ComponentPlacement.RELATED)
																										.addComponent(
																												btn_remove_selected))
																						.addComponent(
																								jsp2)
																						.addComponent(
																								scroll_attachment,
																								GroupLayout.PREFERRED_SIZE,
																								0,
																								Short.MAX_VALUE)
																						.addGroup(
																								pn_headerLayout
																										.createSequentialGroup()
																										.addGroup(
																												pn_headerLayout
																														.createParallelGroup(
																																GroupLayout.Alignment.BASELINE)
																														.addComponent(
																																lbl_from)
																														.addComponent(
																																tf_from,
																																GroupLayout.PREFERRED_SIZE,
																																GroupLayout.DEFAULT_SIZE,
																																GroupLayout.PREFERRED_SIZE))
																										.addPreferredGap(
																												LayoutStyle.ComponentPlacement.RELATED)
																										.addGroup(
																												pn_headerLayout
																														.createParallelGroup(
																																GroupLayout.Alignment.BASELINE)
																														.addComponent(
																																lbl_to)
																														.addComponent(
																																tf_to,
																																GroupLayout.PREFERRED_SIZE,
																																GroupLayout.DEFAULT_SIZE,
																																GroupLayout.PREFERRED_SIZE))
																										.addPreferredGap(
																												LayoutStyle.ComponentPlacement.RELATED)
																										.addGroup(
																												pn_headerLayout
																														.createParallelGroup(
																																GroupLayout.Alignment.BASELINE)
																														.addComponent(
																																lbl_subject)
																														.addComponent(
																																tf_subject,
																																GroupLayout.PREFERRED_SIZE,
																																GroupLayout.DEFAULT_SIZE,
																																GroupLayout.PREFERRED_SIZE)))
																						.addComponent(
																								btn_send,
																								GroupLayout.DEFAULT_SIZE,
																								93,
																								Short.MAX_VALUE))
																		.addGap(0,
																				0,
																				Short.MAX_VALUE)))
										.addContainerGap()));
	}

	private void pn_bodyLayout() {
		pn_body.setBorder(BorderFactory.createTitledBorder(""));

		editor_pane.setContentType("text/html");

		scroll_editor.setViewportView(editor_pane);

		GroupLayout pn_bodyLayout = new GroupLayout(pn_body);
		pn_body.setLayout(pn_bodyLayout);
		pn_bodyLayout.setHorizontalGroup(pn_bodyLayout.createParallelGroup(
				GroupLayout.Alignment.LEADING).addComponent(scroll_editor));
		pn_bodyLayout.setVerticalGroup(pn_bodyLayout.createParallelGroup(
				GroupLayout.Alignment.LEADING).addComponent(scroll_editor,
				GroupLayout.DEFAULT_SIZE, 502, Short.MAX_VALUE));
	}

	private void setMainLayout() {
		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								layout.createParallelGroup(
										GroupLayout.Alignment.LEADING)
										.addComponent(pn_header,
												GroupLayout.Alignment.TRAILING,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addComponent(pn_body,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE))
						.addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(
				GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(pn_header,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(pn_body,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE).addContainerGap()));

		setMinimumSize(new Dimension(1100, 670));
	}

	private void initComponents() {
		newComps();
		setCompsProperties();
		pn_headerLayout();
		pn_bodyLayout();
		setMainLayout();

	}

}
